package dca.sistemascontrole.view;

import dca.sistemascontrole.dao.GraphicDataManager;
import dca.sistemascontrole.domain.DataObject;
import java.awt.Color;
import java.awt.Frame;
import java.util.List;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.RectangleInsets;

/**
 *
 * @author Rafael
 */
public class OfflineChartsDialog extends javax.swing.JDialog {

    private GraphicDataManager dataManager;
    private final long TAMANHO_JANELA = 5 * 60 * 1000;

    public OfflineChartsDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public OfflineChartsDialog(GraphicDataManager dataManager, Frame owner, boolean modal) {
        this(owner, modal);
        this.dataManager = dataManager;
        
        setTanqueSuperior();
        setTanqueInferior();
        setSinalControleGrafico();
    }

    private void setTanqueSuperior() {
        TimeSeries timeSeries = new TimeSeries("t - TanqueSuperior");
        List<DataObject> nivelSinalControle = dataManager.getNivelTanqueSuperior();
        for (DataObject dataObject : nivelSinalControle) {
            timeSeries.addOrUpdate(dataObject.getMillisecond(), dataObject.getValue());
        }

        JFreeChart jfreechart = createChart(new TimeSeriesCollection(timeSeries), "Tanque Superior", "", "");
        ChartPanel pnlChart = new ChartPanel(jfreechart);
        shiftableTanqueSuperior.setChartPanel(TAMANHO_JANELA, pnlChart);
    }

    private void setTanqueInferior() {
        TimeSeries timeSeries = new TimeSeries("t - TanqueInferior");
        List<DataObject> nivelSinalControle = dataManager.getNivelTanqueInferior();
        for (DataObject dataObject : nivelSinalControle) {
            timeSeries.addOrUpdate(dataObject.getMillisecond(), dataObject.getValue());
        }

        JFreeChart jfreechart = createChart(new TimeSeriesCollection(timeSeries), "Tanque Inferior", "", "");
        ChartPanel pnlChart = new ChartPanel(jfreechart);
        shiftableTanqueInferior.setChartPanel(TAMANHO_JANELA, pnlChart);
    }

    private void setSinalControleGrafico() {
        TimeSeries timeSeries = new TimeSeries("t - SinalControle");
        List<DataObject> nivelSinalControle = dataManager.getNivelSinalControle();
        for (DataObject dataObject : nivelSinalControle) {
            timeSeries.addOrUpdate(dataObject.getMillisecond(), dataObject.getValue());
        }

        JFreeChart jfreechart = createChart(new TimeSeriesCollection(timeSeries), "Sinal de Controle", "", "");
        ChartPanel pnlChart = new ChartPanel(jfreechart);
        shiftableSinalControle.setChartPanel(TAMANHO_JANELA, pnlChart);
    }

    private JFreeChart createChart(XYDataset xydataset, String title, String timeAxisLabel, String valueAxisLabel) {
        JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(title, timeAxisLabel, valueAxisLabel, xydataset, false, true, false);
        XYPlot xyplot = (XYPlot) jfreechart.getPlot();

        xyplot.setBackgroundPaint(Color.white);
        xyplot.setRangeGridlinePaint(Color.gray);
        xyplot.setDomainGridlinePaint(Color.gray);
        xyplot.setOutlineVisible(false);
        xyplot.setInsets(new RectangleInsets(0, 5, 5, 0));

        xyplot.setDomainPannable(true);
        xyplot.setRangePannable(false);
        xyplot.setDomainCrosshairVisible(true);
        xyplot.setRangeCrosshairVisible(true);

        jfreechart.setBackgroundPaint(Color.WHITE);

        return jfreechart;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        shiftableTanqueSuperior = new br.ufrn.siga.component.chart.ShiftableChart();
        shiftableTanqueInferior = new br.ufrn.siga.component.chart.ShiftableChart();
        shiftableSinalControle = new br.ufrn.siga.component.chart.ShiftableChart();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setLayout(new java.awt.GridLayout(0, 1));

        shiftableTanqueSuperior.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.add(shiftableTanqueSuperior);

        shiftableTanqueInferior.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.add(shiftableTanqueInferior);

        shiftableSinalControle.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.add(shiftableSinalControle);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private br.ufrn.siga.component.chart.ShiftableChart shiftableSinalControle;
    private br.ufrn.siga.component.chart.ShiftableChart shiftableTanqueInferior;
    private br.ufrn.siga.component.chart.ShiftableChart shiftableTanqueSuperior;
    // End of variables declaration//GEN-END:variables
}
